﻿using System;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;

namespace CodePlex.PropertyDesigner
{
    /// <summary>
    /// Property Designer package.
    /// </summary>
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [ProvideToolWindow(typeof(PropertyDesignerToolWindowPane))]
    [Guid(Constants.PackageGuidString)]
    public sealed class PropertyDesignerPackage : Package
    {
        /// <summary>
        /// Called to initalize this package.
        /// </summary>
        protected override void Initialize()
        {
            // call base
            base.Initialize();

            // get services
            var menuCommandService = GetService(typeof(IMenuCommandService)) as IMenuCommandService;
            if (menuCommandService == null)
                return;

            // create the Show-Property-Designer command
            var commandId = new CommandID(
                Constants.CommandSetGuid,
                (int)Constants.ShowPropertyDesignerCommandId);
            var command = new MenuCommand(
                ShowPropertyDesigner,
                commandId);
            menuCommandService.AddCommand(command);
        }

        /// <summary>
        /// Called to show the property designer.
        /// </summary>
        private void ShowPropertyDesigner(object sender, EventArgs e)
        {
            // get/create the tool window
            var pane = FindToolWindow(
                typeof(PropertyDesignerToolWindowPane),
                0,
                true) as PropertyDesignerToolWindowPane;
            if (pane == null || pane.Frame == null)
                throw new NotSupportedException(Resources.CannotCreateWindow);

            pane.PropertyDesignerControl.ClassDesignerData = new ClassDesignerDataSample();

            // show
            var windowFrame = (IVsWindowFrame)pane.Frame;
            ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }
    }
}
